//
//  informationViewController.m
//  TinyFortune
//
//  Created by lc on 16/5/26.
//  Copyright © 2016年 lc. All rights reserved.
//

#import "informationViewController.h"
#import "personMessageController.h"
#import "NameViewController.h"//昵称
#import "SecretViewController.h"//密码
#import "SexViewController.h"//性别
#import "AreaViewController.h"//区域
#import "IndividualityDescViewController.h"//个性签名
#import "AVFile +  getDictionary.h"

@interface informationViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
//    personMessageController *_name;
   
    /**昵称*/
    NameViewController *_name;
    /**密码*/
    SecretViewController *_secret;
    /**性别*/
    SexViewController *_sex;
    /**区域*/
    AreaViewController *_area;
    /**个性签名*/
    IndividualityDescViewController *_individuality;
    
}
//数据源字典(专门用来存储头像文件的字典)
@property (nonatomic,strong)NSDictionary *dict;

@end

@implementation informationViewController
#pragma mark - 刷新用户信息
- (void)reloadUser{
    AVFile *file = (AVFile *)[[AVUser currentUser] objectForKey:@"iconImage"];
 
    if (file) {
        //如果文件为空的话,用YYModel转化之后就会崩溃
        _dict = [AVFile fileWithDictionary:file];
        NSLog(@"%@",_dict);
    }
    //每次界面将要出现的时候都从服务器上获取用户的数据
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:_dict[@"url"] ]  ] ;
    self.nameLabel.text = [[AVUser currentUser]objectForKey:@"username"];
    
    self.IdLabel.text = [[AVUser currentUser]objectForKey:@"mobilePhoneNumber"];
    self.sexLabel.text = [[AVUser currentUser] objectForKey:@"sex"];
    self.areaLabel.text = [[AVUser currentUser] objectForKey:@"area"];
    self.signatureLabel.text = [[AVUser currentUser]objectForKey:@"signature"];
}
#pragma mark - 生命周期
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //异步刷新
    dispatch_async(dispatch_get_main_queue(), ^{
    [self reloadUser];
    });
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    
}
#pragma mark - UI搭建
- (void)createUI{
    //设置navgationBar
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(onClicked)];
    [self.navigationItem setLeftBarButtonItem:left];
    self.navigationController.navigationBar.translucent = NO ;
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:86/255.0f green:202/255.0f blue:255/255.0f alpha:0.5]];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];

    [self setTitle:@"个人信息"];
    //添加滑动手势
    UISwipeGestureRecognizer *rightSwip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightGo)];
     [self.view addGestureRecognizer:rightSwip];
    [rightSwip setDirection:UISwipeGestureRecognizerDirectionRight];
    
    _name = [NameViewController new];
    
    

    
}
#pragma mark - 添加左滑手势
- (void)rightGo{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onClicked{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 打开相册，照相功能
- (void)takePhoto{
    BOOL ret = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    if (ret) {
        UIImagePickerController *picker = [UIImagePickerController new];
        [picker setSourceType:UIImagePickerControllerSourceTypeCamera];
        picker.delegate = self ;
        [self presentViewController:picker animated:YES completion:nil];
        
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"失败" message:@"科科，主人的手机不支持照相功能啊" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
        [alert show];

    }
}
- (void)openCamera{
    UIImagePickerController *picker = [UIImagePickerController new];
    [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    picker.delegate = self ;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
}
#pragma mark - 实现选中图片的协议
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image = info[@"UIImagePickerControllerOriginalImage"];
    //将图片转换成二进制文件
    NSData *iconData = UIImageJPEGRepresentation(image, 0.01);
   //将二进制文件上传给服务器
    AVFile *file = [AVFile fileWithData:iconData];
    [file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            //上传图片给服务器，更新用户表单中的头像数据
            [[AVUser currentUser]setObject:file forKey:@"iconImage"];
            [[AVUser currentUser] saveInBackground];
            self.iconImageView.image = image ;
            
        }
    }];

    //让图片选择器消失
    [picker dismissViewControllerAnimated:YES completion:nil];
   

}
#pragma mark - Table view data source
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //头像
    if (indexPath.row == 0 && indexPath.section == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *takePhoto = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self takePhoto];
        }];
        UIAlertAction *chosePhtoo = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self openCamera];
        }];
        [alert addAction:chosePhtoo];
        [alert addAction:takePhoto];
        [self presentViewController:alert animated:YES completion:nil];
    }
    //昵称
    if (indexPath.row == 1 && indexPath.section == 0) {
      
        _name.titleName = @"昵称";
        [self.navigationController pushViewController:_name animated:YES];
      
    }
    //密码
    if (indexPath.row == 3 && indexPath.section == 0) {
        _secret = [SecretViewController new];
        _secret.titleName = @"密码";
        [self.navigationController pushViewController:_secret animated:YES];
        
    }
    //性别
    if (indexPath.row == 0 && indexPath.section == 1) {
        
        _sex = [SexViewController new] ;
        _sex.titleName = @"性别";
        [self.navigationController pushViewController:_sex animated:YES];
        
    }
    //地区
    if (indexPath.row == 1 && indexPath.section == 1) {
        
        _area = [AreaViewController new];
        _area.titleName = @"地区";
        [self.navigationController pushViewController:_area animated:YES];
        
    }
    //个性签名
    if (indexPath.row == 2 && indexPath.section == 1) {
        
        _individuality = [IndividualityDescViewController new];
        _individuality.titleName = @"个性签名";
        [self.navigationController pushViewController:_individuality animated:YES];
        
    }
}

@end
